package com.tstudy.laoshibang.custom;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.utils.CommonUtil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Layout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;


@SuppressLint({ "Recycle", "DrawAllocation" })
public class BorderTextView extends TextView {
	boolean mBorderLeft;
	boolean mBorderTop;
	boolean mBorderRight;
	boolean mBorderBottom;
	public int mBorderColor;
	public int mBorderWidth;
	int width;
	public BorderTextView(Context context,int colorRes) {
		super(context);
//		setShadowLayer(2, 0, -1, Color.argb(0x80, 0x00, 0x00, 0x00));
		mBorderColor = BaseApplication.getResColor(colorRes);
		mBorderLeft = true;
		mBorderTop = true;
		mBorderRight = true;
		mBorderBottom = true;
		mBorderWidth = CommonUtil.dip2px(1);
//		setSingleLine(true);
		setGravity(Gravity.CENTER);
//		setTextColor(Color.WHITE);
	}

	public BorderTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		 TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.BorderTextView);
		 mBorderColor = typedArray.getColor(R.styleable.BorderTextView_borderColor, 0);
		 mBorderLeft = typedArray.getBoolean(R.styleable.BorderTextView_borderLeft,true);
		 mBorderTop = typedArray.getBoolean(R.styleable.BorderTextView_borderTop, true);
		 mBorderRight = typedArray.getBoolean(R.styleable.BorderTextView_borderRight,true);
		 mBorderBottom = typedArray.getBoolean(R.styleable.BorderTextView_borderBottom,true);
		 mBorderWidth = (int) typedArray.getDimension(R.styleable.BorderTextView_borderWidth, 0);
	}

	public BorderTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
	}

	@Override
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		Paint paint = new Paint();
		paint.setColor(mBorderColor);
		// 画TextView的4个边
//		if(mBorderLeft){
//			canvas.drawLine(0, 0, 0, this.getHeight() - mBorderWidth, paint);//左边
//		}
//		if(mBorderTop){
//			canvas.drawLine(0, 0, this.getWidth() - mBorderWidth, 0, paint);//上边
//		}
//		if(mBorderRight){
//			canvas.drawLine(this.getWidth() - mBorderWidth, 0, this.getWidth() - mBorderWidth,this.getHeight() - mBorderWidth, paint);//右边
//		}
//		if(mBorderBottom){
//			canvas.drawLine(0, this.getHeight() - mBorderWidth, this.getWidth() - mBorderWidth,this.getHeight() - mBorderWidth, paint);//下边
//			
//		}
		
		if(mBorderLeft){
			canvas.drawRect(0, 0, mBorderWidth, this.getHeight(), paint);//左边
		}
		if(mBorderTop){
			canvas.drawRect(0, 0, this.getWidth(), mBorderWidth, paint);//上边
		}
		if(mBorderRight){
			canvas.drawRect(this.getWidth() - mBorderWidth, 0, this.getWidth(),this.getHeight(), paint);//右边
		}
		if(mBorderBottom){
			canvas.drawRect(0, this.getHeight() - mBorderWidth, this.getWidth(),this.getHeight(), paint);//下边
			
		}
	}
	/**
	 * 只有计算宽度的时候才用到
	 * 
	 * @param text
	 */
	public void setCustomText(CharSequence text,int padding) {
		TextPaint paint = this.getPaint();
		this.width = (int) Layout.getDesiredWidth(text, paint) + padding;
		if (text == null || text.length() <= 0) {
			this.width = 0;
		}
		this.setText(text);
	}

	/**
	 * 获得该View的显示宽度 (px)
	 * 
	 * @return int
	 */
	public int getViewWidth() {
		return width;
	}
	
	public void setViewWidth(int width){
		this.width = width;
	}

}
